package com.tzinfo.tzb;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.tzinfo.data.TztData;
import com.tzinfo.tzt.R;
import com.tzinfo.tzt.util.HttpUtils;
import com.tzinfo.tzt.util.ResultMessage_NoData;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bethinner on 2014/11/27.
 */
public class EdiPostActivity extends Activity{
    TextView tv_subject;
    TextView tv_message;
    TextView tv_up;
    int pid;
    int fid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edi_post);

        tv_subject =(TextView)findViewById(R.id.edi_post_Subject);
        tv_message=(TextView)findViewById(R.id.edi_post_Message);
        tv_up=(TextView)findViewById(R.id.edi_post_up);
        Intent intent = getIntent();
        tv_subject.setText(intent.getExtras().getString("Subject"));
        tv_message.setText(intent.getExtras().getString("Message"));
        pid = intent.getExtras().getInt("pid");
        fid = intent.getExtras().getInt("fid");
        tv_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UpTask upTask = new UpTask();
                upTask.execute();
            }
        });


    }
    public class UpTask extends AsyncTask<String,Void,ResultMessage_NoData> {

        @Override
        protected ResultMessage_NoData doInBackground(String... params) {
            List<NameValuePair> pairs = new ArrayList<NameValuePair>();
            pairs.add(new BasicNameValuePair("pid",String.valueOf(pid) ));
            pairs.add(new BasicNameValuePair("Fid",String.valueOf(fid) ));
            pairs.add(new BasicNameValuePair("Subject", tv_subject.getText().toString()) );
            pairs.add(new BasicNameValuePair("Message",tv_message.getText().toString()) );
            pairs.add(new BasicNameValuePair("sessionid", TztData.userInfo.getData().getSessionID()));
            String GetDataUrl = "http://tzt.tzinfo.net/mob/api/ForumpostUpdate.ashx";
            String DataString = HttpUtils.doPostMethod(GetDataUrl, pairs);
            Gson gson = new Gson();
            ResultMessage_NoData result = gson.fromJson(DataString, ResultMessage_NoData.class);
            return result;
        }

        @Override
        protected void onPostExecute(ResultMessage_NoData resultMessage_noData) {
            super.onPostExecute(resultMessage_noData);
            //操作成功的话应该返回上个界面同时让上个界面重新加载数据
            if (resultMessage_noData.getSuccess()){
                Toast.makeText(EdiPostActivity.this, "操作成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                //把返回数据存入Intent
                intent.putExtra("Reflash", true);
                //设置返回数据
                EdiPostActivity.this.setResult(RESULT_OK, intent);
                //关闭Activity
            EdiPostActivity.this.finish();
           }
        }
    }




    public void goback(View view) {
        this.finish();
    }
}
